﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.UsersWebService;

namespace Cotrobaie
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonRegister.Attributes.Add("onMouseOut", "this.src='/images/register.png'");
            ButtonRegister.Attributes.Add("onMouseOver", "this.src='/images/oregister.png'");
            ButtonLogin.Attributes.Add("onMouseOut", "this.src='/images/login.png'");
            ButtonLogin.Attributes.Add("onMouseOver", "this.src='/images/ologin.png'");

            if (Page.IsPostBack)
            {
                if (Session["RegisterLoginClick"] != null)
                    if (Session["RegisterLoginClick"].ToString() == "l")
                    { RegularExpressionValidator1.Validate(); RegularExpressionValidator2.Validate(); }
                    else if (Session["RegisterLoginClick"].ToString() == "r") { REVUsernameBone.Validate(); REVNumeBona.Validate(); REVPrenumeBona.Validate(); REVEmailBona.Validate(); REVParola.Validate(); CVParola2Bona.Validate(); }
            }
        }
        protected void UsernameBone_TextChanged(object sender, EventArgs e)
        {
            CuVUsernameBone.IsValid = NonPublic.GetUser(TextBoxUserName.Text) == null; if (!CuVUsernameBone.IsValid) ErrorMessage.Text = "Username deja existent. Va rugam alegeti altul";
            CuVEmailBona.IsValid = NonPublic.GetUserByEmail(TextBoxEmail.Text) == null; if (!CuVEmailBona.IsValid) ErrorMessage.Text = "Adresa de E-mail exista deja in baza noastra de date. <br>Daca ati uitat numele de utilizator sau parola <br>puteti accesa pagina de <a href='RecupereazaParola.aspx'>recuperare a parolei</a>";
            Session["RegisterLoginClick"] = "r";
            Page_Load(sender, e);
        }
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            Session["RegisterLoginClick"] = "l";
            Page_Load(sender, e);
            if (Page.IsValid)
            {
                using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.Login(TextBoxUserLogin.Text, TextBoxPasLogin.Text);
                    if (result.Success)
                    {
                        var user = result.User;
                        if (user != null)
                        {
                            Session["UserID"] = user.ID;
                            Session["UserUsername"] = user.Username;
                            Session["UserNume"] = user.Nume;
                            Session["UserPrenume"] = user.Prenume;
                            Session["UserParola"] = user.Parola;
                            Session["UserEmail"] = user.Email;
                            string redirect = Session["LastPageVisited"] != null ? Session["LastPageVisited"].ToString() : "Home.aspx";
                            Session["LastPageVisited"] = null;
                            Response.Redirect(redirect);
                        }
                        else { Literal1.Text = CustomValidator1.ErrorMessage = "Username inexistent"; CustomValidator1.IsValid = false; }
                    }
                    else { Literal1.Text = CustomValidator1.ErrorMessage = result.Mesaj; CustomValidator1.IsValid = false; }
                }
            }
        }

        protected void ButtonSignupForm_Click(object sender, EventArgs e)
        {
            Session["RegisterLoginClick"] = "r";
            UsernameBone_TextChanged(sender, e);
            CVCodVerificare.IsValid = Session["CaptchaImageText"].ToString().ToLower() == TextBoxCapcha.Text.ToLower();
            if (!CVCodVerificare.IsValid) ErrorMessage.Text = "Codul Captcha este gresit";
            if (CuVUsernameBone.IsValid && CuVEmailBona.IsValid && CVCodVerificare.IsValid)
            {
                using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.Register(TextBoxUserName.Text, TextBoxParola.Text, TextBoxNume.Text, TextBoxPrenume.Text, TextBoxEmail.Text);
                    CustomValidator2.IsValid = result.Success;
                    if (!result.Success)
                        ErrorMessage.Text = CustomValidator2.ErrorMessage = result.Mesaj;
                    else
                    {
                        TextBoxUserName.Text = TextBoxParola.Text = TextBoxNume.Text = TextBoxPrenume.Text = TextBoxEmail.Text = TextBoxConfirmPassword.Text = String.Empty;
                        signupform.Visible = loginform.Visible = false;
                        RegisterOk.Visible = true; Mesaj.InnerHtml = result.Mesaj ;
                    }
                }
            }
        }

        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }


        public void tabClick(object sender, EventArgs e)
        {
            ImageButton but = (ImageButton)sender;

            if (but.ID == "ButtonLogin")
            {
                signupform.Visible = false;
                loginform.Visible = true;
            }

            else 
            {
                signupform.Visible = true;
                loginform.Visible = false;
            }

        }
    }
}